package com.ptu.mapper;

import com.github.pagehelper.Page;
import com.ptu.entity.Order;
import com.ptu.entity.OrderItem;
import com.ptu.vo.OrderVO;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    @Insert("INSERT INTO `order` (" +
            "user_id, order_no, total_amount, actual_amount, " +
            "status, payment_method, address_id, remark, " +
            "create_time, update_time" +
            ") VALUES (" +
            "#{userId}, #{orderNo}, #{totalAmount}, #{actualAmount}, " +
            "#{status}, #{paymentMethod}, #{addressId}, #{remark}, " +
            "NOW(), NOW()" +
            ")")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void insert(Order order);

    //按创建时间，越晚创建的越靠前
    @Select("select * from `order` where user_id = #{userId} order by create_time desc")
    List<OrderVO> listOrders(Integer userId);


    @Select("select * from `order` where id = #{id}")
    Order detail(Long id);

    @Update("update `order` set status = 1, update_time = NOW(), pay_time = NOW() where id = #{id}")
    void updatePayStatus(Long id);

    @Update("update `order` set status = 2, update_time = NOW() where id = #{id}")
    void updateCancelStatus(Long id);

    @Update("update `order` set status = 4, update_time = NOW() where id = #{id}")
    void updateConfirmStatus(Long id);

    @Update("update `order` set status = 6, update_time = NOW() where id = #{id}")
    void updateReturnStatus(Long id);

    @Update("update `order` set status = 3, update_time = NOW() where id = #{id}")
    void updateDeliverStatus(Long id);

    Page<OrderVO> adminGetOrderList(Integer status, String paymentMethod);

    @Update("update `order` set status = 7, update_time = NOW() where id = #{id}")
    void updateCommentStatus(Long id);

    @Update("update `order` set status = 5, update_time = NOW() where id = #{id}")
    void updateAdminReturnStatus(Long id);

    @Select("SELECT o.*, c.id as comment_id, c.user_id as comment_user_id, c.order_id as comment_order_id, " +
            "c.content, c.rating, c.images, c.is_anonymous, " +
            "c.create_time as comment_create_time, c.update_time as comment_update_time " +
            "FROM `order` o LEFT JOIN `comment` c ON o.id = c.order_id AND c.user_id = #{userId} " +
            "WHERE o.user_id = #{userId} ORDER BY o.create_time DESC")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "orderNo", column = "order_no"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "status", column = "status"),
            @Result(property = "actualAmount", column = "actual_amount"),
            @Result(property = "comment.id", column = "comment_id"),
            @Result(property = "comment.userId", column = "comment_user_id"),
            @Result(property = "comment.orderId", column = "comment_order_id"),
            @Result(property = "comment.content", column = "content"),
            @Result(property = "comment.rating", column = "rating"),
            @Result(property = "comment.images", column = "images"),
            @Result(property = "comment.isAnonymous", column = "is_anonymous"),
            @Result(property = "comment.createTime", column = "comment_create_time"),
            @Result(property = "comment.updateTime", column = "comment_update_time")
    })
    List<OrderVO> listOrdersWithComments(Integer userId);

    @Select("SELECT o.*, c.id as comment_id, c.user_id as comment_user_id, c.order_id as comment_order_id, " +
            "c.content, c.rating, c.images, c.is_anonymous, " +
            "c.create_time as comment_create_time, c.update_time as comment_update_time " +
            "FROM `order` o LEFT JOIN `comment` c ON o.id = c.order_id " +
            "WHERE o.status = 7 ORDER BY o.create_time DESC")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "orderNo", column = "order_no"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "status", column = "status"),
            @Result(property = "actualAmount", column = "actual_amount"),
            @Result(property = "comment.id", column = "comment_id"),
            @Result(property = "comment.userId", column = "comment_user_id"),
            @Result(property = "comment.orderId", column = "comment_order_id"),
            @Result(property = "comment.content", column = "content"),
            @Result(property = "comment.rating", column = "rating"),
            @Result(property = "comment.images", column = "images"),
            @Result(property = "comment.isAnonymous", column = "is_anonymous"),
            @Result(property = "comment.createTime", column = "comment_create_time"),
            @Result(property = "comment.updateTime", column = "comment_update_time")
    })
    List<OrderVO> listOrdersWithCommentsByStatus();

    @Update("update `order` set status = 1, update_time = NOW() where order_no = #{orderNo}")
    void updatePayStatusByOrderNo(String orderNo);

    // 查询指定时间范围内的每日销售额
    @Select("SELECT DATE_FORMAT(o.create_time, '%Y-%m-%d') as date, " +
            "SUM(oi.price * oi.quantity) as amount " +
            "FROM `order` o " +
            "JOIN order_item oi ON o.id = oi.order_id " +
            "WHERE o.create_time BETWEEN #{startDate} AND #{endDate} " +
            "GROUP BY DATE_FORMAT(o.create_time, '%Y-%m-%d') " +
            "ORDER BY date")
    List<Map<String, Object>> selectSalesTrend(@Param("startDate") Date startDate,
                                               @Param("endDate") Date endDate);

    // 查询各商品类别的销售额
    @Select("SELECT " +
            "CASE p.category_id " +
            "   WHEN 1 THEN '鲜肉' " +
            "   WHEN 2 THEN '蔬菜' " +
            "   WHEN 3 THEN '水果' " +
            "   ELSE '其他' " +
            "END as category, " +
            "SUM(oi.price * oi.quantity) as amount " +
            "FROM order_item oi " +
            "JOIN `order` o ON oi.order_id = o.id " +
            "JOIN product p ON oi.product_id = p.id " +
            "GROUP BY p.category_id")
    List<Map<String, Object>> selectCategorySales();
}
